package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.OKManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 89311 on 2018/4/4.
 */

public class ShdzAdapter extends RecyclerView.Adapter<ShdzAdapter.LinerViewHolder> {
    private Context mContext;
    private  AddressActivity add;
    private JSONObject job;
    private JSONArray jsonArray;
    private int i = 0;

    public ShdzAdapter(Context context, AddressActivity add, JSONArray jsonArray){
        this.mContext = context;
        this.add = add;
        this.jsonArray = jsonArray;
    }

    @Override
    public LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new LinerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.wz_shdzlb_item_view,parent,false));
    }

    @Override
    public void onBindViewHolder(final LinerViewHolder holder, final int position) {
        String phone = null;
        String address = null;
        String receiver_name = null;
        String isdefault = null;
        String user_id = "";
        String id = "";
        String province = "";
        String city = "";
        String district = "";
            try {
                job = (JSONObject) jsonArray.get(i);
                 phone = job.get("receiver_phone").toString();
                 address = job.get("address").toString();
                 receiver_name = job.get("receiver_name").toString();
                 isdefault = job.get("isdefault").toString();
                 user_id = job.get("user_id").toString();
                province = job.get("province").toString();
                city = job.get("city").toString();
                district = job.get("district").toString();
                 id = job.get("id").toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        holder.usernametv.setText(receiver_name);
        holder.accounttv.setText(phone);
        holder.addresstv.setText(province+city+district+address);
        if(isdefault.equals("1")){
            holder.xuanzetv.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.adxz));
        }else{
            holder.xuanzetv.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.adwxz));
        }
        final String finalId = id;
        final String finalUser_id = user_id;
        holder.xuanzetv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OKManager.getInstance().type1022(String.valueOf(finalUser_id), String.valueOf(finalId), new OKManager.func4() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        showToast((Activity) mContext,"修改成功!");
                    }

                    @Override
                    public void onFailure(String string) {
                        showToast((Activity) mContext,"网络错误");
                    }
                });
                refresh();
            }
        });

        final String finalId1 = id;

        holder.shanchutv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder= new AlertDialog.Builder(mContext);
                //标题文字
                builder.setTitle("确定要删除吗?");
                //添加一个确定按钮，打印log发现是 i=-1
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                       OKManager.getInstance().type1023(String.valueOf(finalId1), new OKManager.func4() {
                           @Override
                           public void onResponse(JSONObject jsonObject) {
                               try {
                                   showToast((Activity) mContext,jsonObject.get("message").toString());
                               } catch (JSONException e) {
                                   e.printStackTrace();
                               }
                           }

                           @Override
                           public void onFailure(String string) {

                           }
                       });
                        refresh();
                    }
                });
                //添加一个取消按钮，打印log 发现是 i=-2
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                //设置头像Icon
                builder.show();

            }
        });
        holder.bianjitv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext,BjAdderssActivity.class);
                try {
                    intent.putExtra("bjdz",  jsonArray.get(position).toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mContext.startActivity(intent);
            }
        });
            //holder.xuanzetv.get
        i++;
    }

    @Override
    public int getItemCount() {
        if(jsonArray!=null) {
            return jsonArray.length();
        }else{
            return 0;
        }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private TextView usernametv;
        private TextView accounttv;
        private TextView addresstv;
        private ImageView xuanzetv;
        private TextView shanchutv;
        private TextView bianjitv;

        public LinerViewHolder(View itemView) {
            super(itemView);
            usernametv = itemView.findViewById(R.id.adusername);
            accounttv = itemView.findViewById(R.id.adaccount);
            addresstv = itemView.findViewById(R.id.adaddress);
            xuanzetv = itemView.findViewById(R.id.adxuanze);
            shanchutv = itemView.findViewById(R.id.adsctext);
            bianjitv = itemView.findViewById(R.id.adbjtext);

        }
    }

    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    private void refresh() {
        add.finish();
        Intent intent = new Intent(mContext, AddressActivity.class);
        add.startActivity(intent);
    }


}
